package org.springblade.pm.service.impl;

import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springblade.core.boot.file.BladeFile;
import org.springblade.core.boot.file.BladeFileUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.pm.entity.File;
import org.springblade.pm.mapper.FileMapper;
import org.springblade.pm.service.IFileService;
import org.springblade.pm.tool.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@AllArgsConstructor
public class FileServiceImpl extends BaseServiceImpl<FileMapper,File> implements IFileService {

	/**
	 * 批量保存文件
	 *
	 * @param files
	 * @param file
	 * @return
	 */
	@Override
	public boolean saveBatch(MultipartFile[] files, File file) {
		if (!FileUtil.isWordOrOther(files)) {
			return false;
		}
		List<File> files1 = new ArrayList<>();
		List<BladeFile> bladeFiles = BladeFileUtil.getFiles(Arrays.asList(files));
		File fileReal;
		for (BladeFile bladeFile : bladeFiles) {
			fileReal = new File();
			bladeFile.transfer();
			fileReal.setParentId(file.getParentId());
			fileReal.setOriginalName(bladeFile.getOriginalFileName());
			fileReal.setName(bladeFile.getUploadPath().substring(bladeFile.getUploadPath().lastIndexOf("/") + 1));
			fileReal.setType(BladeFileUtil.getFileExt(bladeFile.getOriginalFileName()));
			fileReal.setPath(bladeFile.getUploadPath());
			fileReal.setModuleType(file.getModuleType());
			files1.add(fileReal);
		}
		return super.saveBatch(files1);
	}

	/**
	 * 批量删除文件
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public boolean removeBatchByIds(String ids) {
		//根据id查询文件储存路径
		List<Integer> idList = Func.toIntList(ids);
		File file;
		for (Integer id : idList) {
			file = super.getById(id);
			//删除对应资源
			FileUtil.delFile(file.getPath());
		}
		return super.removeByIds(Func.toIntList(ids));
	}
	/**
	 * 文件下载
	 *
	 * @param response
	 * @param id
	 * @return
	 */
	@Override
	public void downloadFile(HttpServletResponse response, String id) {
		//根据id查询文件储存路径
		if (StringUtils.isNotBlank(id)) {
			File file = super.getById(id);
			FileUtil.downloadFile(response, file.getPath());
		}
	}

	@Override
	public void preView(HttpServletResponse response, String id) {
		//根据id查询文件储存路径
		if (StringUtils.isNotBlank(id)) {
			File file = super.getById(id);
			FileUtil.preView(response, file.getPath());
		}
	}

	/**
	 * 批量下载
	 *
	 * @param response
	 * @param ids
	 */
	@Override
	public void downloadBatch(HttpServletResponse response, String ids, String zipName) {
		if (StringUtils.isNotBlank(ids)) {
			List<Integer> idList = Func.toIntList(ids);
			List<String> pathList = new ArrayList<>();
			for (Integer id : idList) {
				File file = super.getById(id);
				pathList.add(file.getPath());
			}
			FileUtil.downloadBatch(response, pathList,zipName);
		}
	}
}
